package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.AccountMessage;
import com.itheima.entity.ProjectFundingApplication;
import com.itheima.mapper.AccountAuthenticationMapper;
import com.itheima.mapper.AccountMessageMapper;
import com.itheima.mapper.ProjectFundingApplicationMapper;
import com.itheima.result.PageResult;
import com.itheima.service.IProjectFundingApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 项目资金申请Service业务层处理
 * 
 * @author guanhuaer
 * @date 2025-03-20
 */
@Service
public class ProjectFundingApplicationServiceImpl implements IProjectFundingApplicationService {

    @Autowired
    private ProjectFundingApplicationMapper projectFundingApplicationMapper;
    @Autowired
    private AccountAuthenticationMapper accountAuthenticationMapper;
    @Autowired
    private AccountMessageMapper messageMapper;
    /**
     * 查询项目资金申请
     * 
     * @param id 项目资金申请主键
     * @return 项目资金申请
     */
    @Override
    public ProjectFundingApplication selectProjectFundingApplicationById(Long id) {
        return projectFundingApplicationMapper.selectProjectFundingApplicationById(id);
    }

    /**
     * 查询项目资金申请列表
     * 
     * @param projectFundingApplication 项目资金申请
     * @return 项目资金申请集合
     */
    @Override
    public PageResult selectProjectFundingApplicationList(ProjectFundingApplication projectFundingApplication) {
        PageHelper.startPage(projectFundingApplication.getPageNum(),projectFundingApplication.getPageSize());
        Page<ProjectFundingApplication> page = projectFundingApplicationMapper.selectProjectFundingApplicationList(projectFundingApplication);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增项目资金申请
     * 
     * @param projectFundingApplication 项目资金申请
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProjectFundingApplication(ProjectFundingApplication projectFundingApplication) {
        // 向用户消息表插入一条消息
        AccountMessage message = new AccountMessage();
        message.setUserId(projectFundingApplication.getUserId());
        message.setTitle("你提交了项目资金申请");
        message.setDescription(LocalDate.now().toString() +"你提交了"+
                projectFundingApplication.getProjectName()+"的资金申请，请等待管理员审核");
        message.setDatetime(new Date());
        message.setType(1L);
        message.setIsRead(0L);
        messageMapper.insertAccountMessage(message);
        // 向管理员发送通知
        // 查询管理员id
        List<Long> adminIds = accountAuthenticationMapper.selectAdminIds();
        message.setTitle("有新的 "+projectFundingApplication.getUsername()+" 用户提交了项目资金申请");
        message.setDescription("有新的 "+projectFundingApplication.getUsername()+" 用户提交了"+
                projectFundingApplication.getProjectName()+"项目资金申请"+"请及时查看审核！");
        message.setType(2L);
        message.setStatus("info");
        message.setIsRead(0L);
        message.setExtra("待审核");
        for (Long id : adminIds) {
            message.setUserId(id);
            messageMapper.insertAccountMessage(message);
        }
        // 插入项目资金申请
        projectFundingApplication.setCreateTime(LocalDateTime.now());
        projectFundingApplication.setUpdateTime(LocalDateTime.now());
        projectFundingApplication.setStatus(0);
        return projectFundingApplicationMapper.insertProjectFundingApplication(projectFundingApplication);
    }

    /**
     * 修改项目资金申请
     * 
     * @param projectFundingApplication 项目资金申请
     * @return 结果
     */
    @Override
    public int updateProjectFundingApplication(ProjectFundingApplication projectFundingApplication) {
        // 向用户消息表插入一条消息
        AccountMessage message = new AccountMessage();
        message.setUserId(projectFundingApplication.getUserId());
        message.setTitle("你提交的项目资金申请已审核");
        message.setDescription("你提交的"+projectFundingApplication.getProjectName()+"的资金申请已审核，前往查看审核结果");
        message.setDatetime(new Date());
        message.setType(1L);
        message.setIsRead(0L);
        messageMapper.insertAccountMessage(message);
        projectFundingApplication.setUpdateTime(LocalDateTime.now());
        return projectFundingApplicationMapper.updateProjectFundingApplication(projectFundingApplication);
    }

    /**
     * 批量删除项目资金申请
     * 
     * @param ids 需要删除的项目资金申请主键集合
     * @return 结果
     */
    @Override
    public int deleteProjectFundingApplicationByIds(Long[] ids) {
        return projectFundingApplicationMapper.deleteProjectFundingApplicationByIds(ids);
    }

    /**
     * 删除项目资金申请信息
     * 
     * @param id 项目资金申请主键
     * @return 结果
     */
    @Override
    public int deleteProjectFundingApplicationById(Long id) {
        return projectFundingApplicationMapper.deleteProjectFundingApplicationById(id);
    }

    @Override
    public ProjectFundingApplication selectProjectFundingApplicationByProjectId(Long id) {
        return projectFundingApplicationMapper.selectProjectFundingApplicationByProjectId(id);
    }
}
